package cn.bgxt.callsystemcamera;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class SysVideoCameraActivity extends Activity {
	private Button btn_StartVideoCamera;
	private static final String FILE_PATH = "/sdcard/sysvideocamera.3gp";
	private static final String TAG="main";
	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

	private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sysvideocamera);

		btn_StartVideoCamera = (Button) findViewById(R.id.btn_StartVideoCamera);
		btn_StartVideoCamera.setOnClickListener(click);
	}

	private View.OnClickListener click = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction("android.media.action.VIDEO_CAPTURE");
			intent.addCategory("android.intent.category.DEFAULT");
			File file = new File(FILE_PATH);
			if(file.exists()){
				file.delete();
			}
			Uri uri = Uri.fromFile(file);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
			intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);//??????10?

			startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
		}
	};
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

			if (resultCode == RESULT_OK) {

				// Image captured and saved to fileUri specified in the Intent

				Toast.makeText(this, "Image saved to:\n" +

						data.getData(), Toast.LENGTH_LONG).show();

			} else if (resultCode == RESULT_CANCELED) {

				// User cancelled the image capture

			} else {

				// Image capture failed, advise user

			}

		}



		if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {

			if (resultCode == RESULT_OK) {

				// Video captured and saved to fileUri specified in the Intent

				Toast.makeText(this, "Video saved to:\n" +

						data.getData(), Toast.LENGTH_LONG).show();

			} else if (resultCode == RESULT_CANCELED) {

				// User cancelled the video capture

			} else {

				// Video capture failed, advise user

			}

		}

	}

}
